package com.och.mrcp.core.rtp.session;

import lombok.Data;

/**
 * 抖动缓冲区统计信息
 */
@Data
public class JitterBufferStats {
    
    private int bufferSize;                    // 缓冲区大小
    private int currentSize;                   // 当前缓冲区大小
    private int totalPackets;                  // 总包数
    private int droppedPackets;                // 丢弃的包数
    private int outOfOrderPackets;             // 乱序包数
    private int expectedSequenceNumber;        // 期望的序列号
    private long timeoutMs;                    // 超时时间(毫秒)
    
    /**
     * 计算丢包率
     */
    public double getPacketLossRate() {
        if (totalPackets == 0) {
            return 0.0;
        }
        return (double) droppedPackets / totalPackets * 100.0;
    }
    
    /**
     * 计算乱序率
     */
    public double getOutOfOrderRate() {
        if (totalPackets == 0) {
            return 0.0;
        }
        return (double) outOfOrderPackets / totalPackets * 100.0;
    }
    
    /**
     * 计算缓冲区使用率
     */
    public double getBufferUsageRate() {
        if (bufferSize == 0) {
            return 0.0;
        }
        return (double) currentSize / bufferSize * 100.0;
    }
    
    @Override
    public String toString() {
        return String.format("JitterBufferStats{bufferSize=%d, currentSize=%d, totalPackets=%d, " +
                           "droppedPackets=%d, outOfOrderPackets=%d, packetLossRate=%.2f%%, " +
                           "outOfOrderRate=%.2f%%, bufferUsage=%.2f%%}",
                bufferSize, currentSize, totalPackets, droppedPackets, outOfOrderPackets,
                getPacketLossRate(), getOutOfOrderRate(), getBufferUsageRate());
    }
}
